import * as mongoose from 'mongoose';
import { Provider } from '@nestjs/common';
import { Constants } from '../constants/constants';
import databaseConfig from '../config/database.config';
import { ConfigType } from '@nestjs/config';

export const DatabaseProvider: Provider[] = [
  {
    provide: Constants.CONNECTION.PROVIDER,
    useFactory: (
      dbConfig: ConfigType<typeof databaseConfig>
    ): Promise<typeof mongoose> => {
      return mongoose.connect(`${dbConfig.host}:${dbConfig.port}`, {
        dbName: dbConfig.name,
        user: dbConfig.user,
        pass: dbConfig.pass,
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true
      });
    },
    inject: [databaseConfig.KEY]
  }
];
